﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace CodeNotes.DAL
{
    /// <summary>
    /// 数据访问类:Wx_TemplateImage
    /// </summary>
    public partial class Wx_TemplateImage
    {
        public Wx_TemplateImage()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ImgId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Wx_TemplateImage");
            strSql.Append(" where ImgId=@ImgId");
            SqlParameter[] parameters = {
					new SqlParameter("@ImgId", SqlDbType.Int,4)
			};
            parameters[0].Value = ImgId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CodeNotes.Model.Wx_TemplateImage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Wx_TemplateImage(");
            strSql.Append("TypeId,ImgSrc,Sort,bgWid,bgHigh,qrcodeX,qrcodeY,qrcodeW,qrcodeAngle,qrcodeColor,qrcodeHasBorder,qrcodeIsPng)");
            strSql.Append(" values (");
            strSql.Append("@TypeId,@ImgSrc,@Sort,@bgWid,@bgHigh,@qrcodeX,@qrcodeY,@qrcodeW,@qrcodeAngle,@qrcodeColor,@qrcodeHasBorder,@qrcodeIsPng)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TypeId", SqlDbType.Int,4),
					new SqlParameter("@ImgSrc", SqlDbType.VarChar,150),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@bgWid", SqlDbType.Int,4),
					new SqlParameter("@bgHigh", SqlDbType.Int,4),
					new SqlParameter("@qrcodeX", SqlDbType.Int,4),
					new SqlParameter("@qrcodeY", SqlDbType.Int,4),
					new SqlParameter("@qrcodeW", SqlDbType.Int,4),
					new SqlParameter("@qrcodeAngle", SqlDbType.Int,4),
					new SqlParameter("@qrcodeColor", SqlDbType.VarChar,10),
					new SqlParameter("@qrcodeHasBorder", SqlDbType.Bit,1),
					new SqlParameter("@qrcodeIsPng", SqlDbType.Bit,1)};
            parameters[0].Value = model.TypeId;
            parameters[1].Value = model.ImgSrc;
            parameters[2].Value = model.Sort;
            parameters[3].Value = model.bgWid;
            parameters[4].Value = model.bgHigh;
            parameters[5].Value = model.qrcodeX;
            parameters[6].Value = model.qrcodeY;
            parameters[7].Value = model.qrcodeW;
            parameters[8].Value = model.qrcodeAngle;
            parameters[9].Value = model.qrcodeColor;
            parameters[10].Value = model.qrcodeHasBorder;
            parameters[11].Value = model.qrcodeIsPng;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CodeNotes.Model.Wx_TemplateImage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Wx_TemplateImage set ");
            strSql.Append("TypeId=@TypeId,");
            strSql.Append("ImgSrc=@ImgSrc,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("bgWid=@bgWid,");
            strSql.Append("bgHigh=@bgHigh,");
            strSql.Append("qrcodeX=@qrcodeX,");
            strSql.Append("qrcodeY=@qrcodeY,");
            strSql.Append("qrcodeW=@qrcodeW,");
            strSql.Append("qrcodeAngle=@qrcodeAngle,");
            strSql.Append("qrcodeColor=@qrcodeColor,");
            strSql.Append("qrcodeHasBorder=@qrcodeHasBorder,");
            strSql.Append("qrcodeIsPng=@qrcodeIsPng");
            strSql.Append(" where ImgId=@ImgId");
            SqlParameter[] parameters = {
					new SqlParameter("@TypeId", SqlDbType.Int,4),
					new SqlParameter("@ImgSrc", SqlDbType.VarChar,150),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@bgWid", SqlDbType.Int,4),
					new SqlParameter("@bgHigh", SqlDbType.Int,4),
					new SqlParameter("@qrcodeX", SqlDbType.Int,4),
					new SqlParameter("@qrcodeY", SqlDbType.Int,4),
					new SqlParameter("@qrcodeW", SqlDbType.Int,4),
					new SqlParameter("@qrcodeAngle", SqlDbType.Int,4),
					new SqlParameter("@qrcodeColor", SqlDbType.VarChar,10),
					new SqlParameter("@qrcodeHasBorder", SqlDbType.Bit,1),
					new SqlParameter("@qrcodeIsPng", SqlDbType.Bit,1),
					new SqlParameter("@ImgId", SqlDbType.Int,4)};
            parameters[0].Value = model.TypeId;
            parameters[1].Value = model.ImgSrc;
            parameters[2].Value = model.Sort;
            parameters[3].Value = model.bgWid;
            parameters[4].Value = model.bgHigh;
            parameters[5].Value = model.qrcodeX;
            parameters[6].Value = model.qrcodeY;
            parameters[7].Value = model.qrcodeW;
            parameters[8].Value = model.qrcodeAngle;
            parameters[9].Value = model.qrcodeColor;
            parameters[10].Value = model.qrcodeHasBorder;
            parameters[11].Value = model.qrcodeIsPng;
            parameters[12].Value = model.ImgId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ImgId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Wx_TemplateImage ");
            strSql.Append(" where ImgId=@ImgId");
            SqlParameter[] parameters = {
					new SqlParameter("@ImgId", SqlDbType.Int,4)
			};
            parameters[0].Value = ImgId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ImgIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Wx_TemplateImage ");
            strSql.Append(" where ImgId in (" + ImgIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CodeNotes.Model.Wx_TemplateImage GetModel(int ImgId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ImgId,TypeId,ImgSrc,Sort,bgWid,bgHigh,qrcodeX,qrcodeY,qrcodeW,qrcodeAngle,qrcodeColor,qrcodeHasBorder,qrcodeIsPng from Wx_TemplateImage ");
            strSql.Append(" where ImgId=@ImgId");
            SqlParameter[] parameters = {
					new SqlParameter("@ImgId", SqlDbType.Int,4)
			};
            parameters[0].Value = ImgId;

            CodeNotes.Model.Wx_TemplateImage model = new CodeNotes.Model.Wx_TemplateImage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CodeNotes.Model.Wx_TemplateImage DataRowToModel(DataRow row)
        {
            CodeNotes.Model.Wx_TemplateImage model = new CodeNotes.Model.Wx_TemplateImage();
            if (row != null)
            {
                if (row["ImgId"] != null && row["ImgId"].ToString() != "")
                {
                    model.ImgId = int.Parse(row["ImgId"].ToString());
                }
                if (row["TypeId"] != null && row["TypeId"].ToString() != "")
                {
                    model.TypeId = int.Parse(row["TypeId"].ToString());
                }
                if (row["ImgSrc"] != null)
                {
                    model.ImgSrc = row["ImgSrc"].ToString();
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["bgWid"] != null && row["bgWid"].ToString() != "")
                {
                    model.bgWid = int.Parse(row["bgWid"].ToString());
                }
                if (row["bgHigh"] != null && row["bgHigh"].ToString() != "")
                {
                    model.bgHigh = int.Parse(row["bgHigh"].ToString());
                }
                if (row["qrcodeX"] != null && row["qrcodeX"].ToString() != "")
                {
                    model.qrcodeX = int.Parse(row["qrcodeX"].ToString());
                }
                if (row["qrcodeY"] != null && row["qrcodeY"].ToString() != "")
                {
                    model.qrcodeY = int.Parse(row["qrcodeY"].ToString());
                }
                if (row["qrcodeW"] != null && row["qrcodeW"].ToString() != "")
                {
                    model.qrcodeW = int.Parse(row["qrcodeW"].ToString());
                }
                if (row["qrcodeAngle"] != null && row["qrcodeAngle"].ToString() != "")
                {
                    model.qrcodeAngle = int.Parse(row["qrcodeAngle"].ToString());
                }
                if (row["qrcodeColor"] != null)
                {
                    model.qrcodeColor = row["qrcodeColor"].ToString();
                }
                if (row["qrcodeHasBorder"] != null && row["qrcodeHasBorder"].ToString() != "")
                {
                    if ((row["qrcodeHasBorder"].ToString() == "1") || (row["qrcodeHasBorder"].ToString().ToLower() == "true"))
                    {
                        model.qrcodeHasBorder = true;
                    }
                    else
                    {
                        model.qrcodeHasBorder = false;
                    }
                }
                if (row["qrcodeIsPng"] != null && row["qrcodeIsPng"].ToString() != "")
                {
                    if ((row["qrcodeIsPng"].ToString() == "1") || (row["qrcodeIsPng"].ToString().ToLower() == "true"))
                    {
                        model.qrcodeIsPng = true;
                    }
                    else
                    {
                        model.qrcodeIsPng = false;
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ImgId,TypeId,ImgSrc,Sort,bgWid,bgHigh,qrcodeX,qrcodeY,qrcodeW,qrcodeAngle,qrcodeColor,qrcodeHasBorder,qrcodeIsPng ");
            strSql.Append(" FROM Wx_TemplateImage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ImgId,TypeId,ImgSrc,Sort,bgWid,bgHigh,qrcodeX,qrcodeY,qrcodeW,qrcodeAngle,qrcodeColor,qrcodeHasBorder,qrcodeIsPng ");
            strSql.Append(" FROM Wx_TemplateImage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Wx_TemplateImage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ImgId desc");
            }
            strSql.Append(")AS Row, T.*  from Wx_TemplateImage T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Wx_TemplateImage";
            parameters[1].Value = "ImgId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

